package com.jason.imagesnode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.jason.imagesnode.MatrixToImageWriter;

import javax.imageio.ImageIO;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;

import com.google.zxing.common.HybridBinarizer;

public class Test {

	public static void main(String[] args) {
		
		// 生成二维码
		try {
            
		     String content = "http://news.sohu.com";
		     String path = "/Users/chensihong/Downloads";
		     
		     MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		     
		     Map hints = new HashMap();
		     hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		     BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
		     File file1 = new File(path,"abcqc.jpg");
		     MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
		     
		 } catch (Exception e) {
		     e.printStackTrace();
		 }
		
		// 解析二维码内容
		/*
		try {
            MultiFormatReader formatReader = new MultiFormatReader();
			String filePath = "/Users/chensihong/Downloads/abcqc.jpg";
			File file = new File(filePath);
			BufferedImage image = ImageIO.read(file);;
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			Binarizer  binarizer = new HybridBinarizer(source);
			BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
			Map hints = new HashMap();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			Result result = formatReader.decode(binaryBitmap,hints);
			
			System.out.println("result = "+ result.toString());
			System.out.println("resultFormat = "+ result.getBarcodeFormat());
			System.out.println("resultText = "+ result.getText());
			            
			} catch (Exception e) {
			e.printStackTrace();
			}
			*/

	}

}
